package test.task.fun.view.util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

/**
 * Это view скролится по-странично
 * 
 * @author dimdim
 * 
 */
public class ScrollView extends HorizontalScrollView {
	/**
	 * Layout, в который нужно добавлять элементы шириной в страницу
	 */
	private LinearLayout rootLayout;

	private int activePageIndex;

	public ScrollView(Context context) {
		super(context);
		init(context);
	}

	public ScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public ScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		rootLayout = new LinearLayoutScroll(context);
		addView(rootLayout);
	}

	private class LinearLayoutScroll extends LinearLayout {
		public LinearLayoutScroll(Context context) {
			super(context);
		}

		public LinearLayoutScroll(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		/**
		 * Делаем размер дочерних элементов первого уровня шириной в страницу
		 */
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

			int pageWidth = getMeasuredWidth();
			int pageHeight = getMeasuredHeight();

			pageWidth = ScrollView.this.getMeasuredWidth();

			if (pageWidth == 0) {
				super.onMeasure(widthMeasureSpec, heightMeasureSpec);
				return;
			}

			for (int i = 0; i < getChildCount(); i++) {
				LinearLayout child = (LinearLayout) getChildAt(i);
				LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				child.setLayoutParams(layoutParams);

				int lMeasureSpec = MeasureSpec.EXACTLY;

				int lChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(pageWidth, lMeasureSpec);
				measureChildWithMargins(child, lChildWidthMeasureSpec, 0, heightMeasureSpec, 0);

			}

			// Set dimensions.
			setMeasuredDimension(resolveSize(pageWidth * getChildCount(), MeasureSpec.UNSPECIFIED),
					resolveSize(pageHeight, MeasureSpec.UNSPECIFIED));

		}

		@Override
		protected void onLayout(boolean pChanged, int pL, int pT, int pR, int pB) {
			int lChildTop = getPaddingTop();
			int lChildBottom = getBottom() - getTop() - getPaddingBottom();
			int lChildLeft = getPaddingLeft();
			int lChildCount = getChildCount();

			for (int i = 0; i < lChildCount; i++) {
				View lChild = getChildAt(i);

				if (lChild.getVisibility() != GONE) {
					int lChildLeftTmp = lChildLeft;
					lChildLeft += lChild.getMeasuredWidth();

					lChild.layout(lChildLeftTmp, lChildTop, lChildLeft, lChildBottom);
				}
			}
		}

	};

	/**
	 * Постраничный скролл
	 */
	@Override
	public boolean onTouchEvent(MotionEvent motionEvent) {
		int lEventAction = motionEvent.getAction();
		// If user has finished scrolling and put is finger up.
		if (lEventAction == MotionEvent.ACTION_UP || lEventAction == MotionEvent.ACTION_CANCEL) {
			int scrollX = getScrollX();
			int pageWidth = getMeasuredWidth();
			float startX = motionEvent.getX();

			// Move to the current page.
			activePageIndex = (scrollX + (pageWidth / 2)) / pageWidth;
			smoothScrollTo(activePageIndex * pageWidth, 0);
			return true;
		}

		return super.onTouchEvent(motionEvent);

	}

	public LinearLayout getRootLayout() {
		return rootLayout;
	}

	public void setRootLayout(LinearLayout rootLayout) {
		this.rootLayout = rootLayout;
	}

	public int getActivePageIndex() {
		return activePageIndex;
	}

	public void setActivePageIndex(int activeIndex) {
		this.activePageIndex = activeIndex;
	}

}
